package com.ruoyi.project.bms.bmsVoucherAssociate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.bms.bmsVoucherAssociate.domain.BmsVoucherAssociate;
import com.ruoyi.project.bms.bmsVoucherAssociate.domain.BmsVoucherAssociateQueryRequest;
import com.ruoyi.project.bms.bmsVoucherAssociate.domain.BmsVoucherModel;
import com.ruoyi.project.bms.bmsVoucherAssociate.service.IBmsVoucherAssociateService;
import com.ruoyi.project.paea.paeaDsTeller.domain.PaeaDsTeller;
import com.ruoyi.project.paea.paeaDsTeller.service.IPaeaDsTellerService;
import com.ruoyi.project.system.user.domain.User;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 登记批次Controller
 *
 * @author ruoyi
 * @date 2020-03-27
 */
@Controller
@RequestMapping("/bms/bmsVoucherAssociate")
public class BmsVoucherAssociateController extends BaseController
{
    private String prefix = "bms/bmsVoucherAssociate";

    @Autowired
    private IBmsVoucherAssociateService bmsVoucherAssociateService;

    @Autowired
    private IPaeaDsTellerService paeaDsTellerService;

    @RequiresPermissions("bms:bmsVoucherAssociate:view")
    @GetMapping()
    public String bmsVoucherAssociate()
    {
        System.out.println("测试");
        return prefix + "/bmsVoucherAssociate";
    }

    /**
     * 查询登记批次列表
     */
    @RequiresPermissions("bms:bmsVoucherAssociate:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BmsVoucherAssociateQueryRequest bmsVoucherAssociateQueryRequest)
    {
        java.util.Date beginTrDate = bmsVoucherAssociateQueryRequest.getBeginTrDate();
        Date endTrDate = bmsVoucherAssociateQueryRequest.getEndTrDate();
        startPage();
        List<BmsVoucherAssociate> list = bmsVoucherAssociateService.list(new QueryWrapper<BmsVoucherAssociate>(bmsVoucherAssociateQueryRequest)
                .ge(beginTrDate!=null,"tr_date", beginTrDate)
                .le(endTrDate!=null,"tr_date", endTrDate)
                .orderByDesc("tr_date")
        );
        return getDataTable(list);
    }

    /**
     * 导出登记批次列表
     */
    @RequiresPermissions("bms:bmsVoucherAssociate:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BmsVoucherAssociate bmsVoucherAssociate)
    {
        List<BmsVoucherAssociate> list = bmsVoucherAssociateService.list(new QueryWrapper<>(bmsVoucherAssociate));
        ExcelUtil<BmsVoucherAssociate> util = new ExcelUtil<BmsVoucherAssociate>(BmsVoucherAssociate.class);
        return util.exportExcel(list, "bmsVoucherAssociate");
    }

    /**
     * 新增登记批次
     */
    @GetMapping("/add")
    public String add()
    {
        //List<PaeaDsTeller> paeaDsTellers = paeaDsTellerService
        return prefix + "/bmsAdd";
        //return prefix + "/add";
    }

    /**
     * 新增保存登记批次
     */
    @RequiresPermissions("bms:bmsVoucherAssociate:add")
    @Log(title = "登记批次", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BmsVoucherAssociate bmsVoucherAssociate)
    {
        return toAjax(bmsVoucherAssociateService.saveOrUpdate(bmsVoucherAssociate));
    }

    /**
     * 修改登记批次
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        BmsVoucherAssociate bmsVoucherAssociate = bmsVoucherAssociateService.getById(id);
        mmap.put("bmsVoucherAssociate", bmsVoucherAssociate);
        return prefix + "/edit";
    }

    /**
     * 修改保存登记批次
     */
    @RequiresPermissions("bms:bmsVoucherAssociate:edit")
    @Log(title = "登记批次", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BmsVoucherAssociate bmsVoucherAssociate)
    {
        return toAjax(bmsVoucherAssociateService.saveOrUpdate(bmsVoucherAssociate));
    }

    /**
     * 删除登记批次
     */
    @RequiresPermissions("bms:bmsVoucherAssociate:remove")
    @Log(title = "登记批次", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(bmsVoucherAssociateService.removeByIds(Arrays.asList(ids.split(","))));
        //return error("已接收，无法修改");
    }

    /**
     * 登记前
     */
    @GetMapping("/preAddTeller")
    public String preAddTeller()
    {
        return prefix + "/bmsAdd";
    }

    /**
     * 补登记前
     */
    @GetMapping("/preAddAgain")
    public String preAddAgain()
    {
        return prefix + "/bmsAddAgain";
    }
    /**
     * 登记前
     */

    @RequiresPermissions("bms:bmsVoucherAssociate:bmsAdd")
    @PostMapping("/listTellers")
    @ResponseBody
    public TableDataInfo listTellers(PaeaDsTeller paeaDsTeller)
    {
        User user = ShiroUtils.getSysUser();
        List<PaeaDsTeller> list = paeaDsTellerService.listNotBms(user,paeaDsTeller);
        return getDataTable(list);
    }

    /**
     * 补登记前查询柜员
     */

    @RequiresPermissions("bms:bmsVoucherAssociate:bmsAddAgain")
    @PostMapping("/listTellersAgain")
    @ResponseBody
    public TableDataInfo listTellersAgain(PaeaDsTeller paeaDsTeller)
    {
        User user = ShiroUtils.getSysUser();
        List<PaeaDsTeller> list = paeaDsTellerService.listBms(user,paeaDsTeller);
        return getDataTable(list);
    }


    @PostMapping("/addBms")
    @ResponseBody
    public AjaxResult addBms(@RequestBody List<BmsVoucherModel> data)
    {
        User user = ShiroUtils.getSysUser();
        //保存登记信息
        boolean flag = bmsVoucherAssociateService.saveBmsVoucherAssociates(user,data,1);
        if(!flag){
            return error();
        }
        return success();
    }

    @PostMapping("/addBmsAgain")
    @ResponseBody
    public AjaxResult addBmsAgain(@RequestBody List<BmsVoucherModel> data)
    {
        User user = ShiroUtils.getSysUser();
        //保存登记信息
        boolean flag = bmsVoucherAssociateService.saveBmsVoucherAssociates(user,data,2);
        if(!flag){
            return error();
        }
        return success();
    }
}
